package com.ymatou.iapi.sellerquery.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery.parameter.GetOrderTradeflowBean;
import com.ymatou.iapi.trading.sellerquery.parameter.entities.OrderTradeflow;
import com.ymatou.iapi.trading.sellerquery.parameter.entities.OrderTradeflowRefund;
import com.ymatou.iapi.trading.sellerquery.parameter.resp.GetOrderTradeflowResp;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;
/**
*获取订单退货退款信息
*FreeMaker Template自动生成代码
*/
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetOrderTradeflow{
	private static GetOrderTradeflowBean getordertradeflowBean;
	private static com.ymatou.iapi.trading.sellerquery.service.GetOrderTradeflowCall getordertradeflowCall;
//	private static com.ymatou.query.seller.trading.api.service.GetOrderTradeflowCall getordertradeflowCall;
	
	private static OrderTradeflow trade;
	private static List<OrderTradeflowRefund> refunds;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取单个订单信息");
	}

	@Before
	public void caseUp() {
		getordertradeflowBean=new GetOrderTradeflowBean();
		getordertradeflowCall=new com.ymatou.iapi.trading.sellerquery.service.GetOrderTradeflowCall();
//		getordertradeflowCall=new com.ymatou.query.seller.trading.api.service.GetOrderTradeflowCall();
		trade = new OrderTradeflow();
		refunds = new ArrayList<OrderTradeflowRefund>();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getordertradeflowCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verify(){
		GetOrderTradeflowResp expectResponse = new GetOrderTradeflowResp();
		ConstructExpectedResponse(expectResponse);
		GetOrderTradeflowResp actualResponse = getordertradeflowCall.getOrderTradeflowResp();
		OrderTradeflow expect = expectResponse.getOrderTradeflow();
		OrderTradeflow actual = actualResponse.getOrderTradeflow();
		if(expect == null){
			DataUtil.verify(expect, actual, "验证OrderTradeflow=null");
		}else{
			DataUtil.verifyDateEquals(expect.getCancelTime(), actual.getCancelTime(),"验证CancelTime");
			DataUtil.verify(expect.getDiscount(), actual.getDiscount(),"验证Discount");
			DataUtil.verify(expect.getDiscountOfThirdParty(), actual.getDiscountOfThirdParty(),"验证DiscountOfThirdParty");
			DataUtil.verify(expect.getFreight(), actual.getFreight(),"验证Freight");
			DataUtil.verify(expect.getOrderId(), actual.getOrderId(),"验证OrderId");
			DataUtil.verify(expect.getPaidAmountOfCash(), actual.getPaidAmountOfCash(),"验证PaidAmountOfCash");
			DataUtil.verify(expect.getPaidAmountOfGift(), actual.getPaidAmountOfGift(),"验证PaidAmountOfGift");
			DataUtil.verifyDateEquals(expect.getPaidTime(), actual.getPaidTime(),"验证PaidTime");
			DataUtil.verify(expect.getPayChannel(), actual.getPayChannel(),"验证PayChannel");
			DataUtil.verify(expect.getPostDiscount(), actual.getPostDiscount(),"验证PostDiscount");
			DataUtil.verify(expect.getPostPaidAmountOfCash(), actual.getPostPaidAmountOfCash(),"验证PostPaidAmountOfCash");
			DataUtil.verify(expect.getPostPaidAmountOfGift(), actual.getPostPaidAmountOfGift(),"验证PostPaidAmountOfGift");
			DataUtil.verifyDateEquals(expect.getPostPaidTime(), actual.getPostPaidTime(),"验证PostPaidTime");
			DataUtil.verify(expect.getSellerCouponAmount(), actual.getSellerCouponAmount(),"验证SellerCouponAmount");
			DataUtil.verify(expect.getSellerCouponCode(), actual.getSellerCouponCode(),"验证SellerCouponCode");
			DataUtil.verify(expect.getTotalPrice(), actual.getTotalPrice(),"验证TotalPrice");
			DataUtil.verify(expect.getTradingStatus(), actual.getTradingStatus(),"验证TradingStatus");
			DataUtil.verify(expect.getYmtCouponAmount(), actual.getYmtCouponAmount(),"验证YmtCouponAmount");
			DataUtil.verify(expect.getYmtCouponCode(), actual.getYmtCouponCode(),"验证YmtCouponCode");
			List<OrderTradeflowRefund> exps = expect.getOrderTradeflowRefundList();
			List<OrderTradeflowRefund> acts = actual.getOrderTradeflowRefundList();
			if(exps == null){
				DataUtil.verify(null, acts, "验证OrderTradeflowRefundList==null");
			}else{
				boolean sameSize = exps.size()==acts.size();
				DataUtil.verify(exps.size(), acts.size(), "验证OrderTradeflowRefundList.size()");
				if(sameSize){
					for(OrderTradeflowRefund exp:exps){
						DataUtil.verify(true, acts.stream().anyMatch(x->x.toString().equals(exp.toString())), "验证\n"+exp.toString()+"\n在OrderTradeflowRefundList中存在");
					}
				}
			}
		}
	}
	
	private void ConstructExpectedResponse(GetOrderTradeflowResp response){
		if(getordertradeflowBean.getOrderId() != 0){
			execute(getordertradeflowBean.getOrderId());
			response.setOrderTradeflow(trade);
		}
	}
	
	
	@SuppressWarnings("static-access")
	private void execute(int orderId){
		DefaultWapper db = new DefaultWapper();
		List<Map> trade = db.executeSql(getOrderTradeflow1(orderId));
		if(!DataUtil.isNullOrEmpty(trade)){
			DataUtil.mapToBean(this.trade, trade.get(0));
			List<Map> refunds = db.executeSql(getOrderTradeflow2(orderId));
			if(false == DataUtil.isNullOrEmpty(refunds)){
				for(Map m:refunds){
					OrderTradeflowRefund refund = new OrderTradeflowRefund();
					DataUtil.mapToBean(refund, m);
					this.refunds.add(refund);
				}
				this.trade.setOrderTradeflowRefundList(this.refunds);
			}else{
				this.trade.setOrderTradeflowRefundList(null);
			}
		}else{
			this.trade = null;
		}
	}
	
	public static String getOrderTradeflow1(int orderId){
		return "SELECT"
				+" a.iOrderId AS orderId,"
				+" a.dPaidTime AS paidTime,"
				+" a.dPostPaidTime AS postPaidTime,"
				+"  a.dCancelTime AS cancelTime,"
				+" a.sYmtCouponCode AS ymtCouponCode,"
				+" a.fYmtCouponAmount AS ymtCouponAmount,"
				+" a.sSellerCouponCode AS sellerCouponCode,"
				+"  a.fSellerCouponAmount AS sellerCouponAmount,"
				+"  a.fFreight AS freight,"
				+"  a.fOrderDiscount AS discount,"
				+"  a.fDiscount AS postDiscount,"
				+"  a.fTotalPrice AS totalPrice,"
				+"  a.iTradingStatus AS tradingStatus,"
				+" b.sPayChannel AS payChannel,"
				+" c.fPaidAmountOfCash AS paidAmountOfCash,"
				+" c.fPaidAmountOfGift AS paidAmountOfGift,"
				+" c.fPostPaidAmountOfCash AS postPaidAmountOfCash,"
				+" c.fPostPaidAmountOfGift AS postPaidAmountOfGift,"
				+" c.fDiscountOfThirdParty as discountOfThirdParty"
				+" FROM"
				+"	YmtRelease.dbo.Ymt_Orders a (NOLOCK)"
				+" LEFT JOIN YmtRelease.dbo.Ymt_TradingInfo b (NOLOCK) ON a.iTradingId = b.iTradingId"
				+" LEFT JOIN YmtRelease.dbo.Ymt_OrderState c (NOLOCK) ON a.iOrderId = c.iOrderId"
				+" WHERE"
				+"	a.iOrderId = "+orderId;
	}
	
	public static String getOrderTradeflow2(int orderId){
		return "SELECT"
				+" 	a.OrderId as orderId,"
				+" 	a.AddTime as addTime,"
				+" 	a.RefundBillNo as refundBillNo,"
				+" 	a.RefundAmount as refundAmount,"
				+" 	b.ProcessResult AS refundChannel"
				+" FROM"
				+" 	YmtRelease.dbo.Ymt_RefundBill a (NOLOCK)"
				+" LEFT JOIN YmtRelease.dbo.Ymt_SettlementInvoice b (NOLOCK) ON a.OrderId = b.OrderId"
				+" AND b.BusinessType = 2"
				+" AND a.RefundBillNo = b.BusinessNo"
				+" WHERE"
				+" 	a.OrderId = "
				+ orderId
				+" AND a.SalesRefundStatus = 10;";
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetOrderTradeflow_001() {
		Logger.start(true,"该订单有退款记录,获取单个订单信息成功");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create4_RefundOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备数据");
            getordertradeflowBean.setOrderId(orderId);
            String param=FormatUtil.beanToHttpString(getordertradeflowBean, true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();			
					
			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(), "验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetOrderTradeflow_002() {
		Logger.start(true,"该订单没有退款记录,获取单个订单信息成功");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create1_M2cOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备数据");
            getordertradeflowBean.setOrderId(orderId);
            String param=FormatUtil.beanToHttpString(getordertradeflowBean, true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();			

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(), "验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetOrderTradeflow_003() {
		Logger.start(true,"订单号不存在,获取单个订单信息成功");
		try {
            int orderId = 404;

            String param="OrderId="+orderId;
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();			
			
			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(), "验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetOrderTradeflow_004() {
		Logger.start(true,"订单号为null,获取单个订单信息成功");
		try {

            String param="";
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();			
			
							
			Logger.verifyEquals(ECode.ERROR400, getordertradeflowCall.getCode(), "验证code");
			Logger.verifyEquals(100, getordertradeflowCall.getBCode(), "验证bcode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}